package cn.tedu.hongbei.pms.controller;

import cn.tedu.commons.pojo.vo.PageData;
import cn.tedu.commons.web.JsonResult;
import cn.tedu.hongbei.pms.pojo.param.AlbumAddNewParam;
import cn.tedu.hongbei.pms.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.hongbei.pms.pojo.vo.AlbumListItemVO;
import cn.tedu.hongbei.pms.pojo.vo.AlbumStandardVO;
import cn.tedu.hongbei.pms.service.IAlbumService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
@RequestMapping("/album/")
@Api(tags = "1.相册管理模块")//给Api文档加上说明
@Slf4j
//我们添加此注解是为了检查参数是否符合，下面我们的方法参数中对象中添加Valid和Validated效果相同，
// 但是如果要判断单参数需要使用到validated
@Validated
public class AlbumController {
    @Autowired
    private IAlbumService iAlbumService;

    //处理异常代码代码移至GlobalExceptionHandler同时加上@RestControllerAdvice使controller可以继承该类
//    @ExceptionHandler
//    public String handleServiceException(ServiceException e){
//        log.warn("程序运行过程中出现了ServiceException，将统一处理！");
//        log.warn("异常信息：{}",e.getMessage());
//        return e.getMessage();
//    }

    @PostMapping("add-new")//这样加了注解只能允许post请求
//    @RequestMapping(value = "add-new", method = {RequestMethod.POST, RequestMethod.GET}) //我们可以通过这样的方式来让他有get和post请求
//    @RequestMapping(value = "add-new", method = RequestMethod.POST) //这样只能允许post请求，我们的method是一个数组，但是只有一个元素可以省略{}
    @ApiOperation("添加相册")//给此请求加上说明
    @ApiOperationSupport(order = 100)//order增加一个序号来方便排序
    //@Valid Validation注解，添加此注解需要Spring validation依赖，此注解为了检查参数是否输入
    public JsonResult addNew(@Valid AlbumAddNewParam albumAddNewParam, @ApiIgnore HttpSession session){//没有@ApiIgnore会显示关于session的api各种信息
//        try {
        iAlbumService.addNew(albumAddNewParam);
//            JsonResult jsonResult = new JsonResult();
//            jsonResult.setState(1);
//            jsonResult.setMessage("添加成功");
        log.info("开始处理【添加相册】的请求，参数：{}",albumAddNewParam);
        return JsonResult.ok();
//        } catch (ServiceException e) {
//            return e.getMessage();
//        } catch (Throwable e){
//            e.printStackTrace();
//            return "添加失败，出现了某种异常";
//
//        }
    }

    @ApiOperation("根据id删除相册")
    @ApiOperationSupport(order = 200)//order增加一个序号来方便排序
    @PostMapping("/{id}/deleteById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long", example = "1"),
    })
    //@RequestParam此注解没什么用，但是在使用Api文档时没有则不会有输入框
    //我们可以换成@PathVariable仍然可以
    //@PathVariable此注解是我们将上面的id改为占位符时可以让此参数知道占位符里的参数就是注解的参数
    public JsonResult deleteById(@Range(min = 1, message = "根据ID删除相册失败，请提交合法的ID值！") @PathVariable Long id){
        iAlbumService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id修改相册")
    @ApiOperationSupport(order = 300)//order增加一个序号来方便排序
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long"),
    })
    @PostMapping("updateById")
    public JsonResult updateById(@Range(min = 1, message = "根据ID修改相册失败，请提交合法的ID值！") @RequestParam Long id,
                                 @Valid AlbumUpdateInfoParam albumUpdateInfoParam){
        iAlbumService.updateById(id, albumUpdateInfoParam);
        return JsonResult.ok();
    }


    @GetMapping("list")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order =420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min = 1, message = "查询相册列表失败，请提供正确的页码值！") Integer pageNum,
                           String queryType) {
        log.debug("开始处理【查询相册列表】的请求，页码：{}", pageNum);
//        if (pageNum == null || pageNum < 1) {
//            pageNum = 1;
//        }
//        PageData<AlbumListItemVO> pageData = iAlbumService.list(pageNum);
        Integer newPageNum = pageNum == null ? 1 : pageNum;
        PageData<AlbumListItemVO> pageData;
        if ("all".equals(queryType)){
            pageData = iAlbumService.list(newPageNum, Integer.MAX_VALUE);
        } else {
            pageData = iAlbumService.list(newPageNum);
        }
        return JsonResult.ok(pageData);
    }

    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)//order增加一个序号来方便排序
    @GetMapping("getStandardById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long", example = "1"),
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询相册详情失败，请提交合法的ID值！")
                                     @RequestParam Long id){
        AlbumStandardVO result = iAlbumService.getStandardById(id);
        return JsonResult.ok(result);
    }


}
